import {defineStore} from 'pinia'
import {getConfig} from '@/api/app'
import config from "@/config";

interface AppSate {
    config: Record<string, any>
}

export const useAppStore = defineStore({
    id: 'appStore',
    state: (): AppSate => ({
        config: {}
    }),
    getters: {
        getWebsiteConfig: (state) => state.config.website || {},
        getLoginConfig: (state) => state.config.login || {},
        getTabbarConfig: (state) => state.config.tabbar || [],
        getStyleConfig: (state) => state.config.style || {},
        getH5Config: (state) => state.config.webPage || {},
        getCopyrightConfig: (state) => state.config.copyright || [],
    },
    actions: {
        getImageUrl(url: string) {
            if (this.config.url == undefined) {
                this.config.url = config.baseUrl
            }

            return url.startsWith('http') ? url : `${this.config.url}${url}`
        },
        async getConfig() {
            if ((await getConfig())?.url == undefined) {
                (await getConfig()).url = config.baseUrl
            }
            this.config = await getConfig()
        }
    }
})
